package pl.edu.agh.portal.application.portlets;

import java.io.IOException;
import java.util.Calendar;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import pl.edu.agh.portal.application.types.Employee;
import pl.edu.agh.portal.application.types.Message;
import pl.edu.agh.portal.application.utils.PortletsUtils;

public class MessageRemovePortlet extends ThesisGenericPortlet {

	private static final String NORMAL_VIEW = "/message_remove_portlet.jsp";
	
	private PortletRequestDispatcher normalView;	
	
	private int messageId = -1;
	private Message message;
	
	public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
		String messageIdString = request.getParameter(PortletsUtils.PARAMETER_MESSAGE_ID);
		
		if (messageIdString != null) {
			messageId = Integer.parseInt(messageIdString);
		}				
		
		//TODO: WS get(session, taskId)
		message = new Message(1, new Employee("Autor", "Login", "Password", Calendar.getInstance().getTime()), "Tytul");
			
		request.setAttribute(PortletsUtils.ATTRIBUTE_MESSAGE_FOR_REMOVE, message);
						
		normalView.include(request, response);
	}
	
	@Override
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
		
		String value = request.getParameter(PortletsUtils.MESSAGE_REMOVE_KEY);		
		if (value == null)	value = "";
		if (value.equals(PortletsUtils.BUTTON_CANCEL)) {
			response.sendRedirect("/portal/portal/message_list.psml?mode=1");
		} else if (value.equals(PortletsUtils.BUTTON_REMOVE)) {
			//TODO: WS modify(session, )			
			response.sendRedirect("/portal/portal/message_list.psml?mode=1");						
		} else {
			value = request.getParameter(PortletsUtils.MESSAGE_MENU_KEY);		
			if (value == null)	value = "";
			if (value.equals(PortletsUtils.BUTTON_CREATE_MESSAGE)) {
				response.sendRedirect("/portal/portal/message_create.psml");
			} else if (value.equals(PortletsUtils.BUTTON_INBOX_MESSAGES)) {
				response.sendRedirect("/portal/portal/message_list.psml?mode=1");
			} else if (value.equals(PortletsUtils.BUTTON_OUTBOX_MESSAGES)) {
				response.sendRedirect("/portal/portal/message_list.psml?mode=2");
			} else {
				response.setPortletMode(PortletMode.VIEW);
			}
		}
	}	
	
	public void init(PortletConfig config) throws PortletException {
		super.init(config);
		normalView = config.getPortletContext().getRequestDispatcher(NORMAL_VIEW);
	}
	
	public void destroy() {
		normalView = null;
		super.destroy();
	}
	
}
